package com.atguigu.gmall;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author: 李旭
 * @create: 2021-07-12 11:31
 **/
public class CompletableFutureDemo2 {

    //异步编排
    public static void main(String[] args) throws Exception{

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                5,
                10,
                2,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10)

        );
        //1:创建新线程    第一个任务 要有返回值
        CompletableFuture<Void> completableFuture = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
            System.out.println("1:" + Thread.currentThread().getName() + "我是第一个任务");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "1";
        },threadPoolExecutor).thenApplyAsync(t -> {
            System.out.println("2:" + Thread.currentThread().getName() +
                    "我是第二个任务：上一个任务的返回值为：" + t);
            return "2";
        },threadPoolExecutor).thenAccept(t -> {
            System.out.println("3:" + Thread.currentThread().getName() +
                    "我是第三个任务：上一个任务的返回值为：" + t);
        });

        CompletableFuture.allOf(completableFuture).join();
    }
}
